﻿@model TestUp.Models.AdminModels.ViewCoursesModel

@{
    ViewBag.Title = "View Courses";
    Layout = "~/Views/Admin/_AdminLayout.cshtml";
}

<script type="text/javascript">
    function destroy(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var id = dataItem.CourseId;

        if (confirm("Deleting this course will result into deleting all tests it contains! Sure?")) {
            var form = document.createElement("form");
            form.setAttribute("method", "post");
            form.setAttribute("action", "DeleteCourse");

            var hiddenField = document.createElement("input");
            hiddenField.setAttribute("type", "hidden");
            hiddenField.setAttribute("name", "CourseId");
            hiddenField.setAttribute("value", id);

            form.appendChild(hiddenField);

            document.body.appendChild(form);
            form.submit();
        }
    }

    function edit(e) {
        e.preventDefault();
        var dataItem = this.dataItem($(e.currentTarget).closest("tr"));
        var id = dataItem.CourseId;

        var url = '@Url.Action("EditCourse", "Admin", new { id = "__id__" })';
        window.location.href = url.replace('__id__', id);
    }
</script>

<h2>ManageCourses</h2>

@*@(Html.Kendo().Grid(Model.Courses)
.Name("coursesGrid")
.Columns(cols =>
    {
        cols.Bound("CourseId").Hidden(true);
        cols.Bound("CourseName");
        cols.Command(cmd => cmd.Custom("Edit").Click("edit"));
        cols.Command(cmd => cmd.Custom("Delete").Click("destroy"));
    })
.DataSource(dataSource=>
dataSource.Ajax()
.ServerOperation(false)
.Model(model=>model.Id(item=>item.CourseId))
))*@

@(Html.Kendo().Grid(Model.Courses)
    .Name("coursesGrid")
    .Columns(cols =>
        {
            cols.Bound("CourseId").Hidden(true);
            cols.Bound("CourseName").Width("75%");
            cols.Command(cmd => cmd.Edit().Text("Edit").CancelText("Cancel").UpdateText("Apply"));
            cols.Command(cmd => cmd.Destroy().Text("Delete"));
        })
    .Editable(editable => editable.Mode(GridEditMode.PopUp))
    .ToolBar(cmd => cmd.Create().Text("Create New"))
    .DataSource(dataSource => dataSource
        .Server()
        .Model(model =>
            {
                model.Id(item => item.CourseId);
                model.Field(field => field.CourseId).Editable(false);
            })
        .Create(create => create.Action("CreateCourse", "Admin"))
        .Update(update => update.Action("EditCourse", "Admin"))
        .Destroy(destroy => destroy.Action("DeleteCourse", "Admin"))
        )
      )